package com.wantupai.translate.utils;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.wantupai.translate.App;
import com.wantupai.translate.bean.EventBean;

import org.greenrobot.eventbus.EventBus;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author luxuchang
 * @date 2019/4/24 0024
 * @description
 */
public class CommonUtils {
    /**
     * 线程
     */
    public static class Executorsa {
        public static final ThreadFactory S_THREAD_FACTORY = new ThreadFactory() {
            private final AtomicInteger mCount = new AtomicInteger(5);

            @Override
            public Thread newThread(@NonNull Runnable runnable) {
                return new Thread(runnable, "" + mCount.getAndIncrement());
            }
        };
        public static Executor executor = new Executor() {

            @Override
            public void execute(@NonNull Runnable command) {

            }
        };

    }

    /**
     * 获取应用的版本名称
     *
     * @param context 上下文
     * @return
     */
    public static String getVersionName(Context context) {
        // 取得包管理器
        PackageManager packageManager = context.getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    context.getPackageName(), PackageManager.GET_ACTIVITIES);
            return packageInfo.versionName;// 取得版本号
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * 开始一个按钮的倒计时
     *
     * @param button                   控件(TextView类型)
     * @param normalTextColorResources 正常字体颜色资源id
     * @param unableTextColorResources 不能点击是字体颜色资源id
     * @param totalTime                倒计时总时长
     * @param content                  倒计时结束后展示文本
     */
    public static void startCountDown(final TextView button,
                                      final int normalTextColorResources,
                                      final int unableTextColorResources,
                                      final int totalTime, final String content) {
        final int normalTextColor = ContextCompat.getColor(button.getContext(), normalTextColorResources);
        final int unableTextColor = ContextCompat.getColor(button.getContext(), unableTextColorResources);
        button.setEnabled(false);
        button.setTextColor(unableTextColor);
        new CountDownTimer(totalTime, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                button.setText((millisUntilFinished / 1000) + "s");
            }

            @Override
            public void onFinish() {
                button.setEnabled(true);
                button.setTextColor(unableTextColorResources);
                button.setText(content);
                button.setVisibility(View.GONE);
                EventBus.getDefault().postSticky(new EventBean());
            }
        }.start();

    }

    /**
     * 复制文本到剪切板
     *
     * @param text 内容
     */
    public static void copyText(String text) {
        ClipboardManager cm = (ClipboardManager) App.getInstance().getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        if (cm != null) {
            cm.setText(TextUtils.isEmpty(text) ? "" : text);
            ToastUtils.show("复制成功");
        }
    }
}
